package showtime.framework.component.loading
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * @project Client
	 * @date 2011-7-19
	 * @author 绿豆蛙
	 * 
	 */
	public class NowLoading extends Sprite
	{
		/**
		 * 加载完毕回调。 
		 */		
		private var callback:Function = null;
		/**
		 * 进度条显示控件，该控件可以继承实现。 
		 */		
		private var sprite:ProgressSprite = null;
		
		public function NowLoading(callback:Function = null, progressSprite:ProgressSprite = null)
		{
			this.callback = callback;
			
			if (progressSprite == null)
			{
				sprite = new ProgressSprite();
							
			}
			else
			{
				sprite = progressSprite;
			
			}			
			
			addChild(sprite);
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		
		private function addedToStageHandler(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		public function update(evt:GroupLoaderEvent):void
		{
			sprite.progress(evt);		
			
			if (evt.fileLoaded == 0 && evt.bytesLoaded == evt.bytesTotal)
			{								
				if (callback != null)
				{
					callback();
				}
			}

		}
		
		/**
		 * 
		 * @param evt
		 * 
		 */		
		public function destroy():void
		{
			if (parent != null)
			{
				parent.removeChild(this);
			}
		}
	
	}
}